package com.tarena.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.marketing.web.mapper.ActivityMapper;
import com.tarena.lbs.pojo.marketing.po.ActivityPO;
import com.tarena.lbs.pojo.marketing.query.ActivityQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;

    public PageInfo<ActivityPO> activityPage(ActivityQuery query) {
        //构造一个查询条件
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",query.getBusinessId());
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        return PageInfo.of(activityMapper.selectList(queryWrapper));
    }

    public void save(ActivityPO poParam) {
        activityMapper.insert(poParam);
    }

    /**
     * 根据活动ID获取活动信息
     * 通过调用数据库访问层的方法，根据指定的活动ID，查询并返回对应的活动信息
     * @param activityId
     * @return
     */
    public ActivityPO getActivityById(Integer activityId) {
        return activityMapper.selectById(activityId);
    }

    public List<ActivityPO> getActivitiesByStores(List<Integer> storeIds) {
        //select * from activity where store_ids in (48,49)
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("shop_ids",storeIds);
        //UNDO FIND_IN_SET(48,shop_ids) or FIND_IN_SET(49,shop_ids)
        //本来 活动可以绑定多个店铺 shop_ids="48,50,27"
        return activityMapper.selectList(queryWrapper);
    }
}
